//#include "sort.h"
#include <mips/regdef.h>

	.text
	.align	2
	.globl	heapSort
	.ent	heapSort

heapSort:
# Creacion del stack frame
	.frame		$fp, 56, ra
	.set		noreorder
	.cpload		t9
	.set		reorder
	subu		sp, sp, 56
	.cprestore	40

	sw		ra,  48(sp)
	sw		$fp, 44(sp)

	move	$fp, sp

# Guarda el parametro en el stack
	sw	a0, 56($fp)		# words
	sw	a1, 60($fp)		# arraySize

	sw	zero, 32($fp)	# i = 0
	sw	zero, 28($fp)	# j = 0
	sw	zero, 24($fp)	# k = 0
	sw	zero, 20($fp)	# padre = 0
	sw	zero, 16($fp)	# cmp = 0

	#for (i = 0; i < arraySize; i++)
for_i:
	lw	t0, 32($fp)	#i
	lw	a1, 60($fp)	#arraysize

	bge	t0, a1, fin

	lw	t0, 32($fp)	# i
	sw	t0, 28($fp)	# j = i

	#for (j = i; j < arraySize; j++)
for_j:
	lw	t0, 28($fp)	#j
	lw	a1, 60($fp)	#arraysize

	bge	t0, a1, inc_i

  	#int padre = ((j - i - 1)/2) + i;
  	lw		t0, 32($fp)	#i
  	lw		t1, 28($fp)	#j
  	addu	t2, zero, 2 #2

  	subu	t0, t1, t0		# j-i
  	subu	t0, t0, 1		# j - i - 1
	div		t0, t2			# (j-i-1)/2

	mflo	v0				# ((j - i - 1)/2)

	lw		t0, 32($fp)		# i
	addu	t0, v0, t0		# ((j - i - 1)/2) + i

	sw		t0, 20($fp)		# padre = ((j - i - 1)/2) + i

	#k = j;
	lw		t0, 28($fp)	# j
	sw		t0, 24($fp)	# k = j

	#int cmp = strcasecmp(words[padre], words[k]);
	lw		t0, 20($fp)	#padre
	lw		t1, 24($fp)	#k

	mul		t0, t0, 4		#[padre]
	mul		t1, t1, 4		#[k]

	lw		a0, 56($fp)	# &words
	addu	t0, a0, t0	# &words[padre]

	lw		a0, 56($fp)	# &words
	addu	t1, a0, t1	# &words[k]

	lw		a0, 0(t0)	#words[padre]
	lw		a1, 0(t1)	#words[k]

	jal		strcasecmp		#strcasecmp(words[padre], words[k]);

	sw		v0, 16($fp)		#cmp = strcasecmp(words[padre], words[k]);

	#while (padre >= i && cmp > 0)
while:
	lw		t0, 32($fp)	#i
	lw		t1, 20($fp)	#padre

	blt		t1, t0, inc_j

	lw		t0, 16($fp)	#cmp
	blez	t0, inc_j

swap:
	#swap(&words[padre], &words[k]);
	lw		t0, 20($fp)		#padre
	lw		t1, 24($fp)		#k

	mul		t0, t0, 4		#[padre]
	mul		t1, t1, 4		#[k]

	lw		a0, 56($fp)	#&words
	addu	t0, a0, t0	#&words[padre]

	lw		a0, 56($fp)	#&words
	addu	t1, a0, t1	#&words[k]

	lw		t2, 0(t0)	#words[padre]
	lw		t3, 0(t1)	#words[k]

	sw		t2, 0(t1)	# words[k] = words[padre];
	sw		t3, 0(t0)	# words[padre] = words[k]

	#k = padre;
	lw		t0, 20($fp)
	sw		t0, 24($fp)

	#padre = ((padre - i - 1)/2) + i;
	lw		t0, 32($fp)	#i
  	lw		t1, 20($fp)	#padre
  	addu	t2, zero, 2 #2

  	subu	t0, t1, t0		# padre-i
  	subu	t0, t0, 1		# padre - i - 1
	div		t0, t2			# (padre-i-1)/2

	mflo	v0				# ((j - i - 1)/2)

	lw		t0, 32($fp)		# i
	addu	t0, v0, t0		# ((padre - i - 1)/2) + i

	sw		t0, 20($fp)		# padre = ((padre - i - 1)/2) + i

	#cmp = strcasecmp(words[padre], words[k]);
	lw		t0, 20($fp)		#padre
	lw		t1, 24($fp)		#k

	mul		t0, t0, 4		#[padre]
	mul		t1, t1, 4		#[k]

	lw		a0, 56($fp)	# &words
	addu	t0, a0, t0	# &words[padre]

	lw		a0, 56($fp)	# &words
	addu	t1, a0, t1	# &words[k]

	lw		a0, 0(t0)	#words[padre]
	lw		a1, 0(t1)	#words[k]

	jal		strcasecmp		#strcasecmp(words[padre], words[k]);

	sw		v0, 16($fp)		#cmp = strcasecmp(words[padre], words[k]);

	b		while

	#j++
inc_j:
	lw		t0, 28($fp)		#j
	addu	t0, t0, 1		#j+1
	sw		t0, 28($fp)		#j++
	b		for_j

	#i++
inc_i:
	lw		t0, 32($fp)		#i
	addu	t0, t0, 1		#i+1
	sw		t0, 32($fp)		#i++
	b		for_i

fin:

# Destruccion del stack frame
	move	sp, $fp
	lw		ra,  48(sp)
	lw		$fp, 44(sp)

	addiu	sp, sp, 56
	jr		ra


	.end	heapSort
